home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / Trench-c / Trench.µ.c < prev    next >
Encoding:
Text File  |  1994-12-04  |  5.5 KB  |  186 lines  |  [TEXT/MMCC]

  1. //• ----------------------------------------------------------------------•//
  2. //• Updated to run in the Think C™ environment by Kenneth A. Long on
  3. //• 12 March 1992.
  4. //• Add MacTraps and ANSI.
  5. //• ----------------------------------------------------------------------•//
  6.  
  7. //• -------------------- Source to "Trench Simulator" ------------------- •//
  8. //• Written 12:00 pm  Jul 16, 1985 by 
  9. //• sdh@joevax.UUCP (Steve Hawley)
  10. //• Posted in uiucdcs:net.sources.mac.
  11. //• This is the source to the trench simulator. It was written in MegaMax C.
  12. //• Note the tabs should be set to 4 spaces.
  13.  
  14. //• Program to simulate the trench from Star Wars.
  15. //• Written 12 July 85 by Steve Hawley in MegaMax C.
  16.  
  17. WindowRecord wRecord;     //• Record for window.
  18. WindowPtr myWindow;        //• Pointer to Record.
  19. Rect screenRect;        //• Rectangle representing window size.
  20.  
  21. int wlines = 0;            //• phase of depth lines.
  22.  
  23. //• ----------------------------------------------------------------------•//
  24.  
  25. //• ----------------------------------------------------------------------•//
  26. //• draws lines to give illusion of depth.
  27. //• ----------------------------------------------------------------------•//
  28.  
  29. Draw_Lines (int offx, int offy, int start) 
  30. {
  31.     int x1 = -200, y1 = -100;
  32.     int z, index;
  33.  
  34.     //• Start is the phase (from 0 to 3)>
  35.     //• The lines are projected by the formulae:
  36.  
  37.     //•     x' = x / z; 
  38.     //•     y' = y / z;
  39.  
  40.     //• offx and offy are offsets for viewPoint changes.
  41.     
  42.     z = 10000;        //• KAL added this to keep from dividing by zero.
  43.                     //• in the built app.  Looks and acts the same.
  44.                     
  45.     for (index = 50 - start; index > 0; index -= 4)
  46.     {
  47.         MoveTo (( x1 - offx) / z, 
  48.                 ( y1 - offy) / z);
  49.         LineTo (( x1 - offx) / z, 
  50.                 (-y1 - offy) / z);
  51.                 
  52.         MoveTo ((-x1 - offx) / z, 
  53.                 ( y1 - offy) / z);
  54.         LineTo ((-x1 - offx) / z, 
  55.                 (-y1 - offy) / z);
  56.     }
  57. }
  58.  
  59. //• ----------------------------------------------------------------------•//
  60. //• Draws the frame of the trench.
  61. //• offx and offy again represent the viewPoint offsets, and it is.
  62. //• Projected using the same formulae as before.
  63. //• ----------------------------------------------------------------------•//
  64.  
  65. SetUp (short offx, short offy) 
  66. {
  67.  
  68.     int x1 = -200, x2 = -100, y1 = -100;
  69.     
  70.     
  71.     //• The 50 sets the far end of the lines.  Over 50 makes no noticable
  72.     //• changes.  Less than 50 brings the ends closer.  Try 4.
  73.     
  74.     MoveTo     (  x1 - offx,              
  75.                y1 - offy);
  76.                
  77.     LineTo ((  x1 - offx)    / 50,     
  78.             (  y1 - offy)    / 50);
  79.             
  80.     LineTo ((  x1 - offx)    / 50,     
  81.             (- y1 - offy)   / 50);
  82.             
  83.     LineTo     (  x1 - offx, -            
  84.                y1 - offy);
  85.     
  86.     MoveTo (   x2 - offx,    - 
  87.                y1 - offy);
  88.                
  89.     LineTo ((  x2 - offx)    / 50,
  90.             (- y1 - offy)   / 50);
  91.     
  92.     MoveTo     (- x1 - offx,              
  93.                y1 - offy);
  94.                
  95.     LineTo ((- x1 - offx)    / 50,     
  96.             (  y1 - offy)   / 50);
  97.             
  98.     LineTo ((- x1 - offx)    / 50,     
  99.             (- y1 - offy)   / 50);
  100.     LineTo  (- x1 - offx,    -            
  101.                y1 - offy);
  102.     
  103.     MoveTo     (- x2 - offx, -
  104.                y1 - offy);
  105.                
  106.     LineTo ((- x2 - offx)    / 50,     
  107.             (- y1 - offy)   / 50);
  108. }
  109.  
  110. //• ----------------------------------------------------------------------•//
  111. //• The objects are animated by using exclusive-or drawing. 
  112.  
  113. //• This way, the same routine to draw can be used to erase, and the new 
  114. //• image can be drawn before the old image is erased to help eliminate 
  115. //• flicker.  
  116.  
  117. //• Thus the the program needs two copies of the offset parameters, one
  118. //• for the new and one for the old.
  119. //• ----------------------------------------------------------------------•//
  120.  
  121. main ()
  122. {
  123.     int offxo = 0, offxn = 0, offyo = 0, offyn = 0;
  124.     Point mouse;
  125.     
  126.     InitGraf (&qd.thePort);            //• Set up quickdraw.
  127.     FlushEvents (everyEvent, 0);     //• Kill previous Events.
  128.     InitWindows ();                    //• Initialize window manager.
  129.     
  130.     InitCursor ();                    //• Ken Long added to get rid of
  131.                                     //• default text edit cursor.
  132.     //• Set window size.
  133.     SetRect (&screenRect, 4, 40, 508, 338); 
  134.     
  135.     //• Draw one from scratch.
  136.     myWindow = NewWindow (&wRecord, &screenRect, "\pTrench",1,0, (WindowPtr)-1L, 1, 0L);
  137.     
  138.     //• Set window parameters.
  139.     SetPort (myWindow);             //• Make the window the current grafport.
  140.     ShowWindow (myWindow);             //• Display window.
  141.  
  142.     //• Set the origin so the center of the screen in (0,0).
  143.     SetOrigin (-252, -149);
  144.     PenMode (patXor);                 //• Set exclusive-or drawing.
  145.     
  146.     SetUp (offxn, offyn);             //• Draw Initial SetUp.
  147.     
  148.     Draw_Lines (offxn, offyn, wlines); //• Draw Initial depth lines.
  149.     
  150.     while (!Button ())                 //• Repeat until Button is clicked.
  151.     {
  152.         offxo = offxn;               //• Put new offsets in old variables.
  153.         offyo = offyn;
  154.         GetMouse (&mouse);             //• get the mouses coordinates.
  155.         
  156.         //• We divide the mouse location by two so we don't go
  157.         //• trough the walls or floor of the trench.
  158.         if (mouse.h /2 < offxn)      //• If the horizontal has changed,
  159.             offxn =  mouse.h /2;    //• store it in the new offset.
  160.         else 
  161.             if (mouse.h /2 > offxn)
  162.                 offxn = mouse.h / 2;
  163.                 
  164.         if (mouse.v /2 < offyn)        //• If the vertical has changed,
  165.             offyn = mouse.v / 2;    //• store it in the new offset.
  166.         else 
  167.             if (mouse.v /2 > offyn)
  168.                 offyn = mouse.v / 2;
  169.                 
  170.         //• If the old offset ain't the same as the new one...
  171.         if ((offxo != offxn) || (offyo != offyn))     
  172.         {                                                                                //• differs from the new, update.
  173.             SetUp (offxn, offyn); //• draw the new setup...
  174.             SetUp (offxo, offyo); //• and erase the old one.
  175.         }
  176.         Draw_Lines (offxo, offyo, wlines); //• Erase the vertical lines.
  177.         wlines++; //• Increment wlines.
  178.         if (wlines > 3) 
  179.             wlines = 0; //• Reset wlines if too big.
  180.             
  181.             //• Draw new set of lines.
  182.             Draw_Lines (offxn, offyn, wlines); 
  183.     }
  184. }
  185.  
  186.